package com.cetcs.kmga.dataView.dao;

import java.util.HashMap;
import java.util.Map;

import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;

/**
 * Created by Administrator on 2018/1/24 0024.
 */
public class DynamicTransactionManager implements PlatformTransactionManager {

    private Map<String, PlatformTransactionManager> targetTransactionManagers = new HashMap<String, PlatformTransactionManager>();

    protected PlatformTransactionManager getTargetTransactionManager() {
        String context = DataSourceContextHolder.getDataSourceType()+"Manager";
        return targetTransactionManagers.get(context);
    }

    public void setTargetTransactionManagers(Map<String, PlatformTransactionManager> targetTransactionManagers) {
        this.targetTransactionManagers = targetTransactionManagers;
    }

    public void commit(TransactionStatus arg0) throws TransactionException {
        getTargetTransactionManager().commit(arg0);
    }

    public TransactionStatus getTransaction(TransactionDefinition arg0)
            throws TransactionException {
        return getTargetTransactionManager().getTransaction(arg0);
    }

    public void rollback(TransactionStatus arg0) throws TransactionException {
        getTargetTransactionManager().rollback(arg0);
    }

}
